package com.lai.qq.util.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @PackageName:com.byd.dilink.api.common.utils
 * @ClassName:DateUtils
 * @author:liu.yalan
 * @date:2020/6/5 11:25
 */
@Slf4j
public class DateUtils {
    private static final SimpleDateFormat ymdhms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat hms = new SimpleDateFormat("HH:mm:ss");
    private static final SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat getYmdhms= new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    private static final SimpleDateFormat mdhm= new SimpleDateFormat("M月d日HH:mm");
    private static final SimpleDateFormat ymdhm= new SimpleDateFormat("yyyy-M-d HH:mm");
    /**
     * String(yyyy-MM-dd HH:mm:ss) to Long
     *
     * @param time
     * @return
     */
    public static Long StringToTimestamp(String time) {

        long times = 0;
        try {
            Date date = ymdhms.parse(time);
            times = date.getTime();
        } catch (Exception e) {
            log.info("StringToTimestamp err::" + e.toString());
        }
        return times;

    }

    public static void main(String[] args) {
        String str="1598637600000";

        System.out.println(str.substring(0,10));
    }
    /*
     * 不带ms的long型时间戳
     */
    public static String GetStringTimeFromLong(long timeStamp) {
        try {
            long time = Long.parseLong(String.valueOf(timeStamp) + "000");
            String d = getYmdhms.format(time);
            return d;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * long时间戳转字符串MM-dd HH:mm
     *
     * @param timeStamp
     * @return
     */
    public static String GetStringTimeFromLong2(long timeStamp) {
        try {
            String d = mdhm.format(timeStamp);
            return d;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * long时间戳转字符串yyyy-M-d HH:mm
     *
     * @param timeStamp
     * @return
     */
    public static String GetYMDHMFromLong(long timeStamp) {
        try {
            String d = ymdhm.format(timeStamp);
            return d;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Date转String
     * @param date
     * @param pattern
     * @return
     */
    public static String formatDate(Date date, Object... pattern) {
        String formatDate = null;
        if (date != null) {
            SimpleDateFormat format;
            if (pattern != null && pattern.length > 0) {
                format=new SimpleDateFormat(pattern[0].toString());
            } else {
                format=ymd;
            }
            formatDate=format.format(date);
        }
        return formatDate;
    }

    public static Date stringToDate(String strDateTime, DateTimeFormatter dtf) {
        return Date.from(LocalDateTime.parse(strDateTime, dtf)
                .atZone(ZoneId.systemDefault()).toInstant());
    }
    public static boolean compareCurTime(long comparetime) {
        return comparetime>System.currentTimeMillis();
    }
}
